package com.chaoyong.higo.activity;

import java.io.File;
import java.io.UnsupportedEncodingException;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.ImageUtils;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.StringUtils;
import com.chaoyong.higo.utils.UploadPictureUtil;
import com.chaoyong.higo.utils.V;
import com.chaoyong.higo.utils.Values;
import com.chaoyong.higo.view.SelectPhotoesPopWin;
import com.chaoyong.higo.view.SelectPhotoesPopWin.OnPhotoSelectedListener;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

/**
 * 我要晒单
 * 
 * @author zk
 */
public class ToShowOrderActivity_bak extends BaseActivity {

	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字

	/** 商品图片 */
	private ImageView show_goods_iv;
	/** 商品信息 */
	private TextView show_goods_tv;
	/** 商品价格 */
	private TextView show_price_tv;
	/** 揭晓时间 */
	private TextView show_konw_times;
	/** 想说啥*/
	private EditText question_input;
	/** 想说啥de title*/
	private EditText title_input;
	/** 确定*/
	private TextView show_make_sure;
	
	
	private LinearLayout add_img_ll;
	private ImageView add_img;
	
	private ImageView[] images = new ImageView[6];
	private Bitmap[] bitmaps = new Bitmap[6];
	private int num = 0;
	
	private String img,title,price,time,expect,goods_id;

	@Override
	public int layoutResID() {
		return R.layout.activity_to_show_rder_bak;
	}

	@Override
	public void findView() {
		findTitleIds();
		
		show_goods_iv = V.f(this, R.id.show_goods_iv);
		show_goods_tv = V.f(this, R.id.show_goods_tv);
		show_price_tv = V.f(this, R.id.show_price_tv);
		show_konw_times = V.f(this, R.id.show_konw_times);
		question_input = V.f(this, R.id.question_input);
		show_make_sure = V.f(this, R.id.show_make_sure);
		title_input = V.f(this, R.id.title_input);
		/*intent.putExtra("goods_id", mList.get(position).getGoods_id());
		intent.putExtra("expect", mList.get(position).getExpect());
		intent.putExtra("img", mList.get(position).getCover_img().get(0));
		intent.putExtra("goods_name", mList.get(position).getGoods_name());
		intent.putExtra("goods_price", mList.get(position).getGoods_price());
		intent.putExtra("lottery_time", mList.get(position).getLottery_time());*/
		
		add_img_ll = V.f(this, R.id.add_img_ll);
		add_img = V.f(this, R.id.add_img);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(80, 80);
		add_img.setLayoutParams(lp);
		add_img.setScaleType(ScaleType.FIT_XY);
		
		images[0] = add_img;
		//add_img.setOnClickListener(this);
		
		images[num].setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				selectPicture();
				//images[num].setClickable(false);
			}
		});
		
		img = getIntent().getStringExtra("img");
		title = getIntent().getStringExtra("goods_name");
		price = getIntent().getStringExtra("goods_price");
		time = getIntent().getStringExtra("lottery_time");
		goods_id = getIntent().getStringExtra("goods_id");
		expect = getIntent().getStringExtra("expect");
	}

	private void findTitleIds() {
		base_left_iv = V.f(this, R.id.base_left_iv);
		base_title_tv = V.f(this, R.id.base_title_tv);
		base_right_tv = V.f(this, R.id.base_right_tv);

		base_title_tv.setText("我要晒单");
		base_right_tv.setVisibility(View.GONE);
	}

	@Override
	public void init() {
		base_left_iv.setOnClickListener(this);
		show_make_sure.setOnClickListener(this);
		
		ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL + img, show_goods_iv);
		show_goods_tv.setText(title);
		show_price_tv.setText(price);
		show_konw_times.setText(time);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.base_left_iv:// title左侧返回
			this.finish();
			break;
		case R.id.show_make_sure:// 确定
			if(TextUtils.isEmpty(title_input.getText().toString())){
				showToast("请输入晒单标题");
				return ;
			}
			if(TextUtils.isEmpty(question_input.getText().toString())){
				showToast("请输入晒单内容");
				return ;
			}
			shaidan();
			break;
		case R.id.add_img:
			
			selectPicture();
			
			/*ImageView iv = new ImageView(this);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			lp.setMargins(5, 0, 0, 0);
			iv.setLayoutParams(lp);
			iv.setImageResource(R.drawable.add_img);
			add_img_ll.addView(iv);*/
			
			break;
		}
	}
	
	/**
	 * 选择相册或相机
	 * 
	 */
	private void selectPicture() {
		// DialogHelper.getSelectDialog(this,
		// getResources().getStringArray(R.array.choice_pic),
		// new DialogInterface.OnClickListener() {
		//
		// @Override
		// public void onClick(DialogInterface dialog, int which) {
		// showToast("" + which);
		// UploadPictureUtil.goToSelectPicture(PersonalDataActivity.this,
		// which);
		// }
		// }).setPositiveButton(null, null).show();
		SelectPhotoesPopWin photoesPopWin = new SelectPhotoesPopWin.Builder(this, new OnPhotoSelectedListener() {

			@Override
			public void onPhotoSelected(int which) {
				UploadPictureUtil.goToSelectPicture(ToShowOrderActivity_bak.this, which);
			}
		}).build();
		photoesPopWin.showPopWin(this);
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != Activity.RESULT_OK)
			return;
		switch (requestCode) {
		case ImageUtils.REQUEST_CODE_GETIMAGE_BYCAMERA:
			// 拍照后裁剪
			UploadPictureUtil.startActionCrop(this, UploadPictureUtil.origUri);
			break;
		case ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP:
			// 选图后裁剪
			UploadPictureUtil.startActionCrop(this, data.getData());
			break;
		case ImageUtils.REQUEST_CODE_GETIMAGE_BYSDCARD:
			uploadNewPhoto(images[num]);
			break;
		}
	}
	
	private void uploadNewPhoto(ImageView imageView) {
		Bitmap bitmap = null;
		File file = null;
		if (!StringUtils.isEmpty(UploadPictureUtil.protraitPath) && UploadPictureUtil.protraitFile.exists()) {
			UploadPictureUtil.protraitBitmap = ImageUtils.loadImgThumbnail(UploadPictureUtil.protraitPath, 400, 250);
			bitmap = UploadPictureUtil.protraitBitmap;
			file = UploadPictureUtil.protraitFile;
		} else {
			showToast("图像不存在，上传失败");
		}
		if (bitmap != null) {
			
			imageView.setImageBitmap(bitmap);
			bitmaps[num] = bitmap;
			//images[num].setClickable(false);
			
			images[num].setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Toast.makeText(ToShowOrderActivity_bak.this, num + "", 0).show();
				}
			});
			
			num = num + 1;
			// TODO 网络请求
			//upload(file);
			if(num == 6){
				return ;
			}
			ImageView iv = new ImageView(this);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(80, 80);
			lp.setMargins(5, 0, 0, 0);
			iv.setLayoutParams(lp);
			iv.setScaleType(ScaleType.FIT_XY);
			iv.setImageResource(R.drawable.add_img);
			add_img_ll.addView(iv);
			images[num] = iv;
			images[num].setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					//Toast.makeText(ToShowOrderActivity.this, num + "", 0).show();
					selectPicture();
					
				}
			});
			
		}
	}

	private void shaidan() {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		JSONObject info = new JSONObject();
		try {
			info.put("uid", PrefUtils.getString(this, "userId", ""));
			info.put("goods_id", goods_id);
			info.put("expect", expect);
			info.put("title", title_input.getText().toString());
			info.put("info", question_input.getText().toString());
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			json.put("info", info);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, OApi.appport_share, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					if(jso.getInt("status") == 1){
						finish();
					}
					showToast(jso.getString("data"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

}
